package com.cq.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cq.health.common.MessageConst;
import com.cq.health.common.RedisConst;
import com.cq.health.entity.PageResult;
import com.cq.health.entity.QueryPageBean;
import com.cq.health.entity.Result;
import com.cq.health.pojo.Setmeal;
import com.cq.health.service.SetmealService;
import com.cq.health.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.UUID;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/5/15 19:47
 * @Description: 套餐控制器
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 上传图片至七牛云
     * @param imgFile 图片
     * @return
     */
    @RequestMapping("/upload")
    //@RequestParam("imgFile")中imgFile要和网页中组件的名称对应
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try {
            //原名
             String fileName = imgFile.getOriginalFilename();
             log.debug("########+getOriginalFilename:{}"+fileName);
             //uuid+_+原名
            String saveUploadName = UUID.randomUUID().toString().replace("-","")+"_"+fileName;
            //使用七牛上传
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),saveUploadName);
            //下发到客户端: http://xxxx/saveUploadName
            String filePath = QiniuUtils.qiniu_img_url_pre+saveUploadName;
            log.debug("########+filePath:{}"+filePath);

            //把文件名存入redis，基于Redis的Set集合
            jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_RESOURCES,saveUploadName);

            return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS,filePath);
        }catch (Exception e){
             e.printStackTrace();
             return new Result(false,MessageConst.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 添加套餐
     * @param setmeal 套餐
     * @param groupGroupIds 检查组id列表
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] groupGroupIds){
        try {
            //把表单提交的图片路径中的域名地址去掉
             log.debug("img url:{},groupGroupIds:{}"+setmeal.getImg(),groupGroupIds);
             String saveName = setmeal.getImg().replace(QiniuUtils.qiniu_img_url_pre,"");
             setmeal.setImg(saveName);
             setmealService.add(setmeal,groupGroupIds);
            log.debug("img url:{}"+setmeal.getImg());
             return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
             e.printStackTrace();
             return new Result(false,MessageConst.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 套餐分页
     * @param queryPageBean 1,5，查询条件
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try {
            pageResult = setmealService.pageQuery(queryPageBean);
        }catch (Exception e){
             e.printStackTrace();
             pageResult = new PageResult(0L,new ArrayList());
        }
        return pageResult;
    }



}
